-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
state/remote/s3: Fix Bug with Assume Role for Federated IAM Account #10067
Conversation
It would be ideal if remote config AND the aws provider both supported an ENV flag for the role, akin to how I don't have a lot of familiarity with the code so I did not pursue it very far. But I could see how an env flag for the role could be enabled for remote config by having a section like this for it: Line 36 in f4cf443
Similarly for the provider the assume_role config could be modified to support an env flag like this
|
I identified a related issue where the s3 remote does not check for errors after obtaining the AWS credentials. This would lead to crashes |
@stack72 I do not see this as an enhancement, but a critical bug fix in order to support federated IAM accounts with remote state. It’s critical because without it, it’s not possible to use use multiple AWS accounts with federated logins, which is the best practice for securing AWS environments |
👍 |
- provide role_arn in backend config to enable assume role Fixes #8739
+1 |
Really looking forward to this feature/bug fix :-) |
+1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, this was overlooked at the original time assume role functionality was added.
@stack72, one thing I think we should add is an |
I've been trying this out since it merged - it looks like this works for initial remote state configuration, but the |
Update: actually the failure case I mentioned above only holds if there's already a remote state in place without the |
@jtopper that's a bug - did you just run remote config again? |
I had an existing set of remote state, and ran remote config specifying a Since I'm hacking on an empty account, I deleted the S3 copy and the local state file, and ran remote config again with a role_arn, after which everything behaves as expected. |
I tried to configure remote state from an ec2 which uses the instance profile mechanism. Allways get access denied. Anyone facing the same problem? |
I was having an issue, but realized I was still using 0.8.6. After upgrading to 0.8.7, I can confirm it is working. @bnf2si take a look at your trust profile. By default an EC2 IAM role just has the ec2 service listed as "trusted" in the "trust relationship" tab. To make this works you need to do 2 things:
But then you need to also add a trust policy to the target role (in this example
Hope this helps. |
I also found a solution now for my usecase. With the following assume role configuration in my terraform files I had to attach a policy to "JenkinsAgentCreator" role to allow assuming itself , otherwise I cannot run from terraform from an EC2 via profile. My example code
|
Problem: Running Terraform from an EC2 instance with an instance profile that allows assuming roles, Terraform can assume another role in the AWS provider block, however, cannot assume another role for remote config using S3 backend with the "role_arn" configuration variable. Workaround: Script that performs an STS assume role (assumes that assumed role has the appropriate s3 privileges), script disables remote config, script reenables remote config with the STS temporary credentials. Note: Terraform will complain when the credentials expire. "Error reloading remote state: ExpiredToken: The provided token has expired." Rerun the script. Run Terraform. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
what
Add missing
role_arn
parameter necessary for federated IAM accounts when using s3 for remote state. The attribute is namedrole_arn
for consistency with AWS provider: https://www.terraform.io/docs/providers/aws/index.html#role_arnusage
testing
I could not see a reasonable way to test this change. The call to get the credentials returns an aws Credential. This does not provide any means of inspecting its internals. Alternatively the
s3Factory
would need to support a way of mocking the GetCredentials callwhy
Fixes #8739